package com.luboyan.CBillbill.controller;

import com.luboyan.CBillbill.advice.TokenVerify;
import com.luboyan.CBillbill.dao.user.following.FollowingGroup;
import com.luboyan.CBillbill.dao.user.following.UserFollowing;
import com.luboyan.CBillbill.service.user.following.FollowingFGroupService;
import com.luboyan.CBillbill.service.user.following.UserFollowingService;
import com.luboyan.CBillbill.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户关注和粉丝管理
 */
@RestController
@RequestMapping("users")
public class UserFollowingController {

    @Autowired
    private UserFollowingService userFollowingService;

    @Autowired
    private FollowingFGroupService followingFGroupService;

    @Autowired
    private TokenVerify tokenVerify;

    /**
     * 添加关注用户
     */
    @PostMapping("/followings")
    public Result<String> addUserFollowings(@RequestBody UserFollowing userFollowing){
        Long userId = tokenVerify.getCurrentUserId();
        userFollowing.setUserId(userId);
        userFollowingService.addUserFollowings(userFollowing);
        return Result.success();
    }

    /**
     * 得到关注用户
     * @return 以分组的形式返回给前端
     */
    @GetMapping("/followings")
    public Result<List<FollowingGroup>> getUserFollowings(){
        Long userId = tokenVerify.getCurrentUserId();
        List<FollowingGroup> result = userFollowingService.getUserFollowings(userId);
        return new Result<>(result);
    }

    /**
     * 得到用户的粉丝
     */
    @GetMapping("/fans")
    public Result<List<UserFollowing>> getUserFans(){
        Long userId = tokenVerify.getCurrentUserId();
        List<UserFollowing> result = userFollowingService.getUserFans(userId);
        return new Result<>(result);
    }

    /**
     * 添加用户关注分组
     * @return 返回新分组的id
     */
    @PostMapping("/following-groups")
    public Result<Long> addUserFollowingGroups(@RequestBody FollowingGroup followingGroup){
        Long userId = tokenVerify.getCurrentUserId();
        followingGroup.setUserId(userId);
        Long groupId = followingFGroupService.addUserFollowingGroups(followingGroup);
        return new Result<>(groupId);
    }

    /**
     * 获取用户关注分组
     */
    @GetMapping("/following-groups")
    public Result<List<FollowingGroup>> getUserFollowingGroups(){
        Long userId = tokenVerify.getCurrentUserId();
        return new Result<>(followingFGroupService.getUserFollowingGroups(userId));
    }

    //一些待完善的：移动关注用户分组；删除一个分组后，分组下的关注用户怎么处理？取消用户关注；获取用户关注时的互粉显示
}
